//
//  MainDefaulSearchVC_V4_Tbv.m
//  ZITHER
//
//  Created by weixin on 2018/11/19.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainDefaulSearchVC_V4_Tbv.h"
#import "MainDefaulSearchTypeTwo_V4_Cell.h"
#import "MainDefaulSearchTypeOne_V4_Cell.h"
#import "MainSeachBrandVC.h"
#import "MainSetPriceVC.h"
#import "MainDefaulSearchVC_V4_Sub.h"


@implementation MainDefaulSearchVC_V4_Tbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 20)];
        self.delegate = self;
        self.dataSource = self;

        [self loadDate];
    }
    return self;
}

/** 网络数据加载 */
-(void)loadDate{
 
    [[serviceDefualt loadGoodsSearchConditions:[searchConditionsModel class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            [self setLoadModel:x.responseData];
        }else{
            [[serviceDefualt loadGoodsList:nil page:0 sort:@"-price" reponseObjctClass:nil]subscribeNext:^(ResponseData* x) {
                [[serviceDefualt loadGoodsSearchConditions:[searchConditionsModel class]]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        
                        [self setLoadModel:x.responseData];
                    }else{
                         [Tools showTips:x.errorInfo];
                    }
                }];
            }];
        }
    }];
}

/** 处理数据 */
-(void)setLoadModel:(searchConditionsModel *)model{
    
    self.model = model;

    if (self.arrM != nil) {
        return;
    }
    
    self.arrM = [[NSMutableArray alloc]init];
    
    
    
    [self.arrM addObject:@"价格范围"];
    
    letterModel *model1 = [[letterModel alloc]init];
    model1.brandName = @"品牌";
    [self.arrM addObject:model1];

    NSArray *arr = model.searchOptionVOS;
    for (searchOptionVOsModel *model1 in arr) {
        NSDictionary *dic = @{@"title":model1.title,@"num":@""};
        [self.arrM addObject:dic];
    }
//    NSArray *arr = model.searchOptionVOS;
    

    
//    for (searchOptionVOsModel *model in arr) {
//        NSMutableArray *arrM1 = [[NSMutableArray alloc]init];
//        optionssModel *model2  = [[optionssModel alloc]init];
//        model2.name = @"不限";
//        [arrM1 addObject:model2];
//
//        for (optionssModel *model1 in  model.options) {
//            [arrM1 addObject: model1];
//        }
//
////        [self.arrM addObject:arrM1];
//
//
//    }
    
    [self reloadData];
    
}




#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.indexType != 1) {
        return self.arrM.count;
    }else{
        return self.arrMSelect.count;
    }
    
    
//    if (self.typeVC) {
//        return self.arrM.count;
//    }else{
//        return 1;
//    }
    
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.indexType == 1) {
        return [self getTypeTwoCell:indexPath];
    }
    
    if (indexPath.row == 0) {
        
            NSString *string = self.arrM[0];
            if ([string isEqualToString:@"价格范围"]) {
                return [self getTypeOneCell:string];
            }else{
                return [self getTypeTwoCell:indexPath];
            }
        
    }else if (indexPath.row == 1){
        letterModel *model = self.arrM[1];
        
        if ([model.brandName isEqualToString:@"品牌"]) {
            return [self getTypeOneCell:model.brandName];
        }else{
            return [self getTypeTwoCell:indexPath];
        }
        
    }else{
       NSDictionary *dic = self.arrM[indexPath.row];
//
        if ([dic[@"num"] isEqualToString:@""]) {
            return [self getTypeOneCell:dic[@"title"]];
        }else{
            return [self getTypeTwoCell:indexPath];
        }
    }
    

  
    
}

-(UITableViewCell *)getTypeOneCell:(NSString*)str{
    static NSString *sectionCell = @"MainDefaulSearchTypeOne_V4_Cell";
    MainDefaulSearchTypeOne_V4_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDefaulSearchTypeOne_V4_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.lab.text = str;
    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
}

-(UITableViewCell *)getTypeTwoCell:(NSIndexPath*)indexPath{
    

    static NSString *sectionCell = @"MainDefaulSearchTypeTwo_V4_Cell";
    MainDefaulSearchTypeTwo_V4_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDefaulSearchTypeTwo_V4_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    NSString *strHead = nil;
    NSString *str = nil;
    
    NSInteger index = indexPath.row;
    
    if (self.indexType != 1) {
        if (index == 0) {
            str = self.arrM[index];
            strHead = @"价格范围";
        }else if(index == 1){
            letterModel *model = self.arrM[index];
            strHead = @"品牌";
            str = model.brandName;
            
        }else{
            NSDictionary *dic = self.arrM[index];
            
            strHead = dic[@"title"];
            
            searchOptionVOsModel *model = self.model.searchOptionVOS[index-2];
            
            str = model.options[[dic[@"num"] intValue]].name;
        }
    }else{
        
        
        NSString *num = self.arrMSelect[index];
        
        
        if ([num isEqualToString:@"0"]) {
            str = self.arrM[0];
            strHead = @"价格范围";
        }else if ([num isEqualToString:@"1"]){
            letterModel *model = self.arrM[1];
            strHead = @"品牌";
            str = model.brandName;
        }else{
      
            NSDictionary *dic = self.arrM[[num intValue]];
            
            strHead = dic[@"title"];

            searchOptionVOsModel *model = self.model.searchOptionVOS[[num intValue]-2];
            
            num = dic[@"num"];

            str = model.options[[num intValue]].name;
        }
    }
   
    
    cell.HeadLab.text = strHead;
    cell.lab.text = str;
    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger index = indexPath.row;
    
    if (self.indexType != 1) {
        if (indexPath.row == 0) {
            [self pushMainSetPriceVC];
            
        }else if (indexPath.row == 1){
            [self pushMainSeachBrandVC];
            
        }else{
            [self pushSearchOptionVOsModel:indexPath.row];
        }
    }else{
        NSString *num = self.arrMSelect[index];
        
        
        if ([num isEqualToString:@"0"]) {
            [self pushMainSetPriceVC];
           
        }else if ([num isEqualToString:@"1"]){
            [self pushMainSeachBrandVC];

        }else{
            NSString *num = self.arrMSelect[index];
            [self pushSearchOptionVOsModel:[num integerValue]];
            }
    }
}

-(void)pushMainSetPriceVC{
    /* TODO: ================= 选择价格 =================*/
    MainSetPriceVC * vc = [[MainSetPriceVC alloc]init];
    [vc setBlcokPrice:^(NSString *str) {
        NSLog(@"%@",str);
        self.arrM[0] =str;
        
        [self reloadData];
        
        NSLog(@"");
        
    }];
    [self.getNav pushViewController:vc animated:YES];
}
-(void)pushMainSeachBrandVC{
    NSLog(@"子条件选择页面");
    MainSeachBrandVC * vc = [[MainSeachBrandVC alloc]init];
    vc.model = self.model;
    vc.modelSelect = self.arrM[1];
    [vc setBlock:^(NSDictionary *dic) {
        letterModel *mdoel2 = [[letterModel alloc]init];
        mdoel2.brandName = dic[@"brandNameNative"];
        mdoel2.id = dic[@"id"];
        self.arrM[1] = mdoel2;
        [self reloadData];
        
        //            self.brandName = dic[@"brandNameNative"];
        //            self.brandId = dic[@"id"];
        //            self.arrM[1] = dic[@"brandNameNative"];
    }];
    [vc setBlockType:^{
        letterModel *mdoel2 = [[letterModel alloc]init];
        mdoel2.brandName =  @"品牌";
        mdoel2.id = @"";
        self.arrM[1] = mdoel2;
        [self reloadData];
    }];
    [self.getNav pushViewController:vc animated:YES];
}

-(void)pushSearchOptionVOsModel:(NSInteger)index{
    NSDictionary *dic1 = [[NSDictionary alloc]initWithDictionary:self.arrM[index]];
    searchOptionVOsModel *model = self.model.searchOptionVOS[index-2];
    MainDefaulSearchVC_V4_Sub *vc = [[MainDefaulSearchVC_V4_Sub alloc]init];
    vc.dic = dic1;
    vc.model = model;
    vc.lab.text = [NSString stringWithFormat:@"搜索条件   >   %@",dic1[@"title"]];
    [vc setBlcok:^(NSDictionary * _Nonnull dic) {
        self.arrM[index] = dic;
        [self reloadData];
    }];
    [self.getNav pushViewController:vc animated:YES];
    
}

//设置区头不悬浮
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    
//    CGFloat sectionHeaderHeight = 50;
//    
//    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
//        
//        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
//        
//    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
//        
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//        
//    }
//    
//}

-(void)setIndexType:(NSInteger)indexType{
    _indexType = indexType;
    [self setSelectarrM];
}

-(void)setSelectarrM{
    
    self.arrMSelect = [[NSMutableArray alloc]init];
    
    if ([self.arrM[0] isEqualToString:@"价格范围"]) {
        [self.arrMSelect addObject:@"0"];
    }
    
    letterModel *model = self.arrM[1];
    
    if ([model.brandName isEqualToString:@"品牌"]) {
        [self.arrMSelect addObject:@"1"];
    }
    
    for (int i = 2 ; i<self.arrM.count; i++) {
        NSString *num = self.arrM[i][@"num"];
        if (num.length !=0) {
            [self.arrMSelect addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    
}
@end
